package program2;
import java.io.*;
import java.net.*;
import program1.MyHashMay;
import program1.filein;
public class MyRunnable implements Runnable {//多线程的实现
	Socket socket;
	public MyRunnable(Socket socket)
	{
		this.socket=socket;
	}			
	@Override
	public void run()
	{
		try
		{	
			
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//从连接通道中获取输入流读取数据
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
			int data;//用于读取数据
			StringBuilder buffer = new StringBuilder();//实现字符的拼接
			String regex = "\\s";//定义正则表达式
			filein.input();//将本地文件中的数据写入表中
			while((data = br.read())!=-1) 
			{	
				
				buffer.append((char)data);	
				String receivedString = buffer.toString();
				if (data=='\n') //判断是否为字符串的结尾
				{
					String []word = receivedString.split(regex);
					System.out.println("接收到的字符串: " + receivedString);
					buffer = new StringBuilder();//重置buffer					
				
					
							if(word[0].equals("help") && (word.length==1 || word.length==2))	
							{
								if(word.length==1)//判断help后面有无command
								{
									String [] help = MyHashMay.help();//调用help
									for (String str : help) //将所以指令返回给客户端
									{
										pw.println(str);					      
									}
								}
								else
								{
									String help = MyHashMay.help(word[1]);
									pw.println(help);
								}
								pw.println("end");
							}		
						
							else if( "ping".equals(word[0]))
							{	
								pw.println("pong");
								pw.println("end");
							}
						
							else if("set".equals(word[0]) && word.length==3)					
							{	
								MyHashMay.set(word[1], word[2]);//调用set						
								filein.output(receivedString);//写入							
								pw.println("1");
								pw.println("end");
							}
						
							else if("get".equals(word[0]) && word.length==2) 
								{
									String value = MyHashMay.get(word[1]);
									pw.println(value);
									pw.println("end");
								}
					
							else if ("del".equals(word[0]) && word.length==2)
								{
									MyHashMay.del(word[1]);
									filein.delete(word[1]);
									pw.println("1");
									pw.println("end");
								}
						
							else if("lpush".equals(word[0]) && word.length==3)
							{			
								MyHashMay.lpush(word[1], word[2]);
								pw.println("1");
								pw.println("end");
							}
						
							else if("rpush".equals(word[0]) && word.length==3)
							{		
								MyHashMay.rpush(word[1], word[2]);
								pw.println("1");
								pw.println("end");
							}
						
							else if("lpop".equals(word[1]) && word.length==2)
									{
										String deled1 = MyHashMay.lpop(word[1]);
										pw.println(deled1);
										pw.println("end");
									}
					
							else if("rpop".equals(word[0]) && word.length==2)
									{
										String deled2 = MyHashMay.rpop(word[1]);
										pw.println(deled2);
										pw.println("end");
									}
					
							else if("len".equals(word[0]) && word.length==2)
									{
										String num = String.valueOf(MyHashMay.len(word[1]));
										pw.println(num);
										pw.println("end");
									}
									
							else if("ldel".equals(word[0]) && word.length==2)
									{
										MyHashMay.ldel(word[1]);									
										pw.println("1");
										pw.println("end");
									}																																									
							else if( "range".equals(word[0])&& word.length==4)
									{
										int start = Integer.parseInt(word[2]);
										int end = Integer.parseInt(word[3]);
										String[] range=MyHashMay.range(word[1], start, end);
										for(String str:range)
										{
											pw.println(str);
										}	
										pw.println("end");												
									}
							else if("hset".equals(word[0])&&word.length==4)
									{
										MyHashMay.hset(word[1],word[2],word[3]);
										pw.println("1");
										pw.println("end");
									}
							else if("hget".equals(word[0])&&word.length==3)
									{
										String value = MyHashMay.hget(word[1],word[2]);
										pw.println(value);										
										pw.println("end");
									}
							else if("hdel".equals(word[0])&&(word.length==3 || word.length==2))
									{
										if(word.length==3)
										{
											MyHashMay.hdel(word[1],word[2]);
										}
										else 
											MyHashMay.hdel(word[1]);
										pw.println("1");
										pw.println("end");
									}
								else
									{	
										pw.println("输入的指令无效");
										pw.println("end");
									}								
					}														
            }					
		}catch(IOException e){  //处理异常
			e.printStackTrace();
		}finally{
			if(socket != null)//进行非空判断
			{
				try 
				{
					socket.close();
				}catch(IOException e){
					e.printStackTrace();			
				}
			}	
		}
			
			
	}
}
	